<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class CreateMenuTable extends AbstractMigration
{
    public function change(): void
    {
        $table = $this->table('menu', [
            'id' => true,
            'primary_key' => ['id'],
            'comment' => '菜单/资源表',
            'engine' => 'InnoDB',
            'encoding' => 'utf8mb4',
        ]);
        $table->addColumn('parent_id', 'integer', [
            'null' => false,
            'default' => 0,
            'comment' => '父级ID',
        ])->addColumn('name', 'string', [
            'limit' => 20,
            'null' => false,
            'default' => '',
            'comment' => '菜单名称',
        ])->addColumn('path', 'string', [
            'limit' => 255,
            'null' => true,
            'comment' => '路由路径',
        ])->addColumn('component', 'string', [
            'limit' => 255,
            'null' => true,
            'comment' => '组件路径',
        ])->addColumn('icon', 'string', [
            'limit' => 50,
            'null' => true,
            'comment' => '图标',
        ])->addColumn('permission', 'string', [
            'limit' => 100,
            'null' => true,
            'comment' => '权限标识',
        ])->addColumn('type', 'enum', [
            'values' => ['menu', 'button', 'api'],
            'null' => false,
            'default' => 'menu',
            'comment' => '类型:menu=菜单,button=按钮,api=接口',
        ])->addColumn('method', 'string', [
            'limit' => 10,
            'null' => true,
            'comment' => '请求方法',
        ])->addColumn('status', 'boolean', [
            'null' => false,
            'default' => 1,
            'comment' => '状态:0=隐藏,1=显示',
        ])->addColumn('sort', 'integer', [
            'null' => false,
            'default' => 0,
            'comment' => '排序',
        ])->addColumn('create_time', 'timestamp', [
            'default' => 'CURRENT_TIMESTAMP',
            'comment' => '创建时间',
        ])->addColumn('update_time', 'timestamp', [
            'default' => 'CURRENT_TIMESTAMP',
            'update' => 'CURRENT_TIMESTAMP',
            'comment' => '更新时间',
        ])->addIndex(['parent_id'], [
            'name' => 'idx_parent_id',
        ])->addIndex(['permission'], [
            'name' => 'uk_permission',
            'unique' => true,
        ])->create();
    }
}
